//
//  YYGestureRecognizer.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/8/16.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

var yy_panDic: [String: [CGFloat]]!

extension UIPanGestureRecognizer {
    
    static func yy_pan() -> UIPanGestureRecognizer {
        let pan = UIPanGestureRecognizer(target: self, action: #selector(yy_panAction(_:)))
        
        if yy_panDic == nil {
            yy_panDic = [:]
        }
        
        yy_panDic[NSString(format: "%p", pan) as String] = [0, 0, 0, 0]
        
        return pan
    }
    
    @objc static func yy_panAction(_ sender: UIPanGestureRecognizer) {
        if sender.state == .began {
            let scale = sqrt(pow(sender.view!.transform.a, 2) + pow(sender.view!.transform.b, 2))
            var rotation = CGFloat(acosf(Float(sender.view!.transform.a / scale)))
            if sender.view!.transform.b < 0 {
                rotation = -rotation
            }
            yy_panDic[NSString(format: "%p", sender) as String] = [sender.view!.transform.tx, sender.view!.transform.ty, scale, rotation]
        } else {
            let rotation = yy_panDic[NSString(format: "%p", sender) as String]![3]
            var translation = sender.translation(in: sender.view)
            let scale = yy_panDic[NSString(format: "%p", sender) as String]![2]
            translation.x *= scale
            translation.y *= scale
            let transform = CGAffineTransform(translationX: yy_panDic[NSString(format: "%p", sender) as String]![0] + translation.x * cos(rotation) - translation.y * sin(rotation), y: yy_panDic[NSString(format: "%p", sender) as String]![1] + translation.y * cos(rotation) + translation.x * sin(rotation))
            var senderTransform = sender.view!.transform
            senderTransform.tx = transform.tx
            senderTransform.ty = transform.ty
            sender.view!.transform = senderTransform
        }
    }
    
}


var yy_pinchDic: [String: [CGFloat]]!

extension UIPinchGestureRecognizer {
    
    static func yy_pinch() -> UIPinchGestureRecognizer {
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(yy_pinchAction(_:)))
        
        if yy_pinchDic == nil {
            yy_pinchDic = [:]
        }
        
        yy_pinchDic[NSString(format: "%p", pinch) as String] = [1, 0]
        
        return pinch
    }
    
    @objc static func yy_pinchAction(_ sender: UIPinchGestureRecognizer) {
        if sender.state == .began {
            let scale = sqrt(pow(sender.view!.transform.a, 2) + pow(sender.view!.transform.b, 2))
            var rotation = CGFloat(acosf(Float(sender.view!.transform.a / scale)))
            if sender.view!.transform.b < 0 {
                rotation = -rotation
            }
            yy_pinchDic[NSString(format: "%p", sender) as String] = [scale, rotation]
        } else {
            var transform = CGAffineTransform(rotationAngle: yy_pinchDic[NSString(format: "%p", sender) as String]![1])
            let scale = yy_pinchDic[NSString(format: "%p", sender) as String]![0] * sender.scale;
            transform = transform.scaledBy(x: scale, y: scale)
            var senderTransform = sender.view!.transform
            senderTransform.a = transform.a
            senderTransform.b = transform.b
            senderTransform.c = transform.c
            senderTransform.d = transform.d
            sender.view!.transform = senderTransform
        }
    }
    
}


var yy_rotationDic: [String: [CGFloat]]!

extension UIRotationGestureRecognizer {
    
    static func yy_rotation() -> UIRotationGestureRecognizer {
        let rotation = UIRotationGestureRecognizer(target: self, action: #selector(yy_rotationAction(_:)))
        
        if yy_rotationDic == nil {
            yy_rotationDic = [:]
        }
        
        yy_rotationDic[NSString(format: "%p", rotation) as String] = [1, 0]
        
        return rotation
    }
    
    @objc static func yy_rotationAction(_ sender: UIRotationGestureRecognizer) {
        if sender.state == .began {
            let scale = sqrt(pow(sender.view!.transform.a, 2) + pow(sender.view!.transform.b, 2))
            var rotation = CGFloat(acosf(Float(sender.view!.transform.a / scale)))
            if sender.view!.transform.b < 0 {
                rotation = -rotation
            }
            yy_rotationDic[NSString(format: "%p", sender) as String] = [rotation, scale]
        } else {
            var transform = CGAffineTransform(rotationAngle: yy_rotationDic[NSString(format: "%p", sender) as String]![0] + sender.rotation)
            transform = transform.scaledBy(x: yy_rotationDic[NSString(format: "%p", sender) as String]![1], y: yy_rotationDic[NSString(format: "%p", sender) as String]![1])
            var senderTransform = sender.view!.transform
            senderTransform.a = transform.a
            senderTransform.b = transform.b
            senderTransform.c = transform.c
            senderTransform.d = transform.d
            sender.view!.transform = senderTransform
        }
    }
    
}
